package com.kasax.test.bc.model.netapi

import com.kasax.hilog.Logger
import com.kasax.test.bc.model.BaseModel
import com.kasax.utils.GsonUtils
import com.kasax.utils.NetHelper.buildRetrofit
import com.kasax.utils.ThreadUtils

class RetrofitModel : BaseModel() {

    override fun testGetIpAddr() {
        ThreadUtils.IO_EXECUTOR.execute {
            val params = mutableMapOf<String, String>()
            params["number"] = "17688888888"
            val call = buildRetrofit("https://cx.shouji.360.cn/").create(INetTestApi::class.java)
                .testGetIpAddr("phonearea.php", params)
            call.execute().body()?.let {
                Logger.log("testGetIpAddr response: ${GsonUtils.deEnconde(it)}}")
            }
        }
    }

    override fun getToabaiIp() {
        ThreadUtils.IO_EXECUTOR.execute {
            val params = mutableMapOf<String, String>()
            params["accessKey"] = "alibaba-inc"
            params["ip"] = "11.11.11.11"
            val call = buildRetrofit("https://ip.taobao.com/").create(INetTestApi::class.java)
                .getToabaiIp("getToabaiIp", params)
            call.execute().body()?.let {
                Logger.log("getToabaiIp response: ${GsonUtils.deEnconde(it)}}")
            }
        }
    }

    override fun getFormUrl() {
        ThreadUtils.IO_EXECUTOR.execute {
            val params = mutableMapOf<String, String>()
            params["param"] = "测试22"
            params["email"] = "crap.cn@gmail.com"
            params["555"] = "我"
            val call = buildRetrofit("http://api.crap.cn/").create(INetTestApi::class.java)
                .getFormUrl("visitor/example/post.do", params)
            call.execute().body()?.let {
                Logger.log("getToabaiIp response: ${GsonUtils.deEnconde(it)}}")
            }
        }
    }

    override fun getJsonParam() {
        ThreadUtils.IO_EXECUTOR.execute {
            val paramsData =
                "{\n" + "\"id\":\"8989-dddvdg\",\n" + "\"name\":\"文章标题-JSON格式参数演示\",\n" + "\"brief\":\"快速入门json参数\",\n" + "\"category\":\"分类\"\n" + "}"
            val headerMap = mutableMapOf<String, String>()
            headerMap["Content-Type"] = "application/json"
            val call = buildRetrofit("http://api.crap.cn/").create(INetTestApi::class.java)
                .getJsonParam("visitor/example/json.do", paramsData, headerMap)
            call.execute().body()?.let {
                Logger.log("getJsonParam response: ${GsonUtils.deEnconde(it)}}")
            }
        }
    }

    override fun getXmlParam() {
        ThreadUtils.IO_EXECUTOR.execute {
            val paramsData = "<users>\n" + "<name>用户名</name>\n" + "<value>value值</value>\n" + "</users>"
            val headerMap = mutableMapOf<String, String>()
            headerMap["Content-Type"] = "application/xml"
            val call = buildRetrofit("http://api.crap.cn/").create(INetTestApi::class.java)
                .getXmlParam("visitor/example/xml.do", paramsData, headerMap)
            call.execute().body()?.let {
                Logger.log("getXmlParam response: ${GsonUtils.deEnconde(it)}}")
            }
        }
    }

    override fun testWanAndroidBanners() {
        ThreadUtils.IO_EXECUTOR.execute {
            val call = buildRetrofit("https://www.wanandroid.com/").create(INetTestApi::class.java)
                .testWanAndroidBanners("banner/json")
            call.execute().body()?.let {
                Logger.log("testWanAndroidBanners response: ${GsonUtils.deEnconde(it)}}")
            }
        }
    }

    override fun testWanAndroidLogin() {
        val params = mutableMapOf<String, String>()
        params["username"] = "zzgtest02"
        params["password"] = "123456"
        ThreadUtils.IO_EXECUTOR.execute {
            val call = buildRetrofit("https://www.wanandroid.com/").create(INetTestApi::class.java)
                .testWanAndroidLogin("user/login", params)
            call.execute().body()?.let {
                Logger.log("testWanAndroidBanners response: ${GsonUtils.deEnconde(it)}}")
            }
        }
    }
}